<?php
class entry extends baseController {    
	public function __construct() {
		parent::__construct ();
	}
    
    public function loadView($temp) {
        if (!isset($temp["data"]))
            $temp["data"] = '';
        $this->load->view("index", $temp);
    }
    
    public function index($slug) {
        $entry = $this->entryModel->read(array('slug' => $slug));
        if(!$entry) {
            redirect(base_url());
            return;   
        }
        
        $this->session->set_userdata('selectedcategory', $entry['categoryid']);
        
        $this->entryModel->update(array('views' => ($entry['views'] + 1)), array('id' => $entry['id']));
        $this->_data["template"] = 'entry';
        $this->_data["data"] = $entry;
        $account = $this->accountModel->read($entry['accountid']);
        $this->_data['username'] = $account['username'];
        
        $comments = $this->commentModel->get_order_where(null, 'id', 'DESC', array('entryid' => $entry['id']));
        
        $result = array();
        $accountcomments = array();
        foreach($comments as $k=>$v) {
            $accountid = $v['accountid'];
            if(!isset($accountcomments[$accountid])) {
                $acc = $this->accountModel->read($accountid);
                $accountcomments[$accountid] = $acc['username'];
            }
            $v['accountid'] = $accountcomments[$accountid];
            $result[] = $v;
        }
        
        $this->_data['comments'] = $result;
        $this->_data['olderentry'] = $this->getOlderEntry($entry, 5);
        
        $sub = $this->categoryModel->read(array('id' => $entry['categoryid']));
        $parent = $this->categoryModel->read(array('id' => $sub['parentid']));
        $this->_data['parent'] = $parent;

        $this->loadView($this->_data);
    }
}
?>